package com.xpsoft.oa.workflow.event;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.SealApply;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.model.system.FileAttach;
import com.xpsoft.oa.service.admin.SealApplyService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.DepartmentService;
import com.xpsoft.oa.service.system.FileAttachService;

public class SealApplyFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(SealApplyFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String userId = (String)pi.getVariable("sealApply.applyUser.userId");
		String depId = (String)pi.getVariable("sealApply.dept.depId");
		String proNeed = (String)pi.getVariable("sealApply.proNeed");
		String proId = (String)pi.getVariable("sealApply.project.id");
		String sealType = (String)pi.getVariable("sealApply.sealType");
		String sealPurpose = (String)pi.getVariable("sealApply.sealPurpose");
		String sealDesc = (String)pi.getVariable("sealApply.sealDesc");
		String remark = (String)pi.getVariable("sealApply.remark");
		String attachIds = (String)pi.getVariable("sealApply.attachIds");
		
		SealApply sealApply = new SealApply();
		sealApply.setProNeed(proNeed);
		sealApply.setSealType(sealType);
		sealApply.setSealDesc(sealDesc);
		sealApply.setRemark(remark);
		sealApply.setSealPurpose(sealPurpose);
		if(StringUtils.isNotBlank(depId)){
			DepartmentService departmentService = (DepartmentService)AppUtil.getBean("departmentService");
			sealApply.setDept(departmentService.get(Long.valueOf(depId)));
		}
		if(StringUtils.isNotBlank(userId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			sealApply.setApplyUser(appUserService.get(Long.valueOf(userId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			sealApply.setProject(projectService.get(Long.valueOf(proId)));
		}
		FileAttachService fileAttachService = (FileAttachService)AppUtil.getBean("fileAttachService");
		if(StringUtils.isNotBlank(attachIds)){
			Set<FileAttach> fileSet = new HashSet<FileAttach>();
			String fileIds[] = attachIds.split(",");
			sealApply.getFileAttachs().clear();
			for(String fileId:fileIds){
				fileSet.add(fileAttachService.get(Long.valueOf(fileId)));
			}
			sealApply.getFileAttachs().addAll(fileSet);
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		sealApply.setProcessRunId(processRun.getRunId());
		
		SealApplyService sealApplyService = (SealApplyService)AppUtil.getBean("sealApplyService");
		sealApplyService.save(sealApply);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
